package com.group3.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.group3.dao.*;
import com.group3.domain.*;
import com.group3.pojo.receive.SLessonReceive;
import com.group3.pojo.result.LessonResult;
import com.group3.service.StudentLessonService;
import com.group3.utils.NotNull;
import com.group3.utils.Result;
import com.group3.utils.ResultUtil;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * StudentLessonServiceImpl
 *
 * @author HY
 * 2021/06/10 16:55
 **/


@Service
public class StudentLessonServiceImpl implements StudentLessonService {
    @Resource
    private LessonMapper lessonMapper;
    @Resource
    private StudentLessonMapper studentLessonMapper;
    @Resource
    private LessonTeacherMapper lessonTeacherMapper;

    @Override//实现
    public Result<List<LessonResult>> getLesson(SLessonReceive receive) {
        LessonExample lessonExample = new LessonExample();
        LessonExample.Criteria criteria = lessonExample.createCriteria();
        StudentLessonExample studentLessonExample = new StudentLessonExample();
        studentLessonExample
                .createCriteria()
                .andStudentIdEqualTo(receive.getStudentId());//通过学号查课程名，学号为必填
        List<StudentLesson> studentLessons
                = studentLessonMapper.selectByExample(studentLessonExample);

        if (studentLessons.isEmpty()){//为空时直接返回
            return ResultUtil.success();
        }

        List<String> lessonNames = new ArrayList<>();//储存课程名的集合
        for (StudentLesson studentLesson : studentLessons){
            lessonNames.add(studentLesson.getLessonName());
        }
        criteria.andLessonNameIn(lessonNames);//获取所有该课程名的课程
        if (NotNull.judge(receive.getStartDateTime())){//传入了时间段
            criteria.andDateGreaterThanOrEqualTo(receive.getStartDateTime());
        }
        if (NotNull.judge(receive.getEndDateTime())){//传入了时间段
            criteria.andDateLessThanOrEqualTo(receive.getEndDateTime());
        }
        if (NotNull.judge(receive.getLessonName())){//传入了课程名
            criteria.andLessonNameEqualTo(receive.getLessonName());
        }
        if (NotNull.judge(receive.getTeacherZ())){//传入了责任老师
            criteria.andTeacherIdEqualTo(receive.getTeacherZ());
        }
        if (NotNull.judge(receive.getTeacherS())){//传入了实训老师，获取该实训老师的所有课程
            LessonTeacherExample lessonTeacherExample = new LessonTeacherExample();
            lessonTeacherExample
                    .createCriteria()
                    .andTeacherIdEqualTo(receive.getTeacherS());
            List<LessonTeacherKey> keys
                    = lessonTeacherMapper.selectByExample(lessonTeacherExample);
            List<String> lessonNames1 = new ArrayList<>();
            for (LessonTeacherKey key : keys){
                lessonNames1.add(key.getLessonName());
            }
            criteria.andLessonNameIn(lessonNames1);
        }

        PageHelper.startPage(receive.getPageNumber(),receive.getPageSize());
        List<LessonResult> lessons = lessonMapper.selectLessonInfo(lessonExample);
        PageInfo<LessonResult> pageInfo = new PageInfo<>(lessons);

        return ResultUtil.success(lessons,pageInfo.getTotal());
    }
}
